var improve="N",month='',day='',birth_date='';
var redirectUrl = $('#redirectUrl').val();
var district_Arr='';
$(document).ready(function(){
	district_Arr=JSON.parse(sessionStorage.getItem('district_Arr'));
	var Region_str='<option value="">-- Please Select Region --</option>';
	for(var i=0;i<district_Arr.length;i++){
		Region_str+='<option value="'+district_Arr[i].id+'">'+district_Arr[i].name+'</option>';
	}
	$('#Region').html(Region_str);

})

$('#Region').change(function () {
	var Region_id=$(this).val();
	if (Region_id==""){
		$('#District').html('<option value="">-- Please Select District -- </option>');
	}
	var District_str='<option value="">-- Please Select District -- </option>';
	for(var i=0;i<district_Arr.length;i++){
		if (district_Arr[i].id == Region_id){
			var district=district_Arr[i].district;
			for(var j=0;j<district.length;j++){
				District_str+='<option value="'+district[j].id+'">'+district[j].name+'</option>';
			}
			$('#District').html(District_str);
			break;
		}
	}
})
//月、日的改变
$('#Month').change(function(){
	month=$('#Month option:selected').val();
	month = ( month.length < 2 ? "0"+month : month );
	var date=getDay(parseInt(month));
	var day_str='<option value="">Day</option>';
	for(var i = 1; i <= date ;i++){
		day_str+='<option value="'+i+'">'+( i<10 ? "0"+i : i)+'</option>';
	}
	$('#Day').html(day_str);
})
$('#Day').change(function() {
	day=$('#Day option:selected').val();
	day = ( day.length < 2 ? "0"+day : day );
	birth_date= (day+'/'+month+'/2000');
})
function getDay(month){
	switch (month){
		case 2:
			return 29;
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
			break;
	}
}
//重新加载验证码
$('.btnBox a.Reload').on('tap',function(){
	var date=new Date();

	$('.validate').html('<img src="http://203.189.170.43/mpj2/Captcha.jpg?'+date.getTime()+'" />');

})
//是否联系你
$('.form-group .check').on('tap',function(){
	if($(this).hasClass('on')){
		$(this).removeClass('on');
		improve="N";
	}else{
		$(this).addClass('on');
		improve="Y";
	}

})

//注册类型选择
$('#shopType').change(function(){
	var val=$('#shopType option:selected').val();
	if(val=="HOME"){
		$('.companyBox').hide();
		$('.isShowDescription').show();

	}else if(val=="BUSINESS"){
		$('.companyBox').show();
		$('.isShowDescription').hide();
	}
})

$('.registerBox input').on('focus',function(){
	if($(this).hasClass('err')){
		$(this).removeClass('err');
	}
})
$('.registerBox input').on('blur',function(){
	if($(this).val()==''){
		$(this).addClass('err');
	}
})

function  isNull(){
	var len=$('.registerBox input').length;
	var slen=$('.registerBox select').length;
	var m=0;
	for(var i=0;i<len;i++){
		var $input=$('.registerBox input').eq(i);
		var $val=$input.val();

		if($val==""){
			m++;
			if($('#shopType option:selected').val()!='BUSINESS'){
				m--;
			}
			$input.addClass('err');
			$input.next("span").html("This field is required");
		}else{
			$input.removeClass('err');
			$input.next("span").html("");
		}
	}

	for(var i=0;i<slen;i++){
		var $input=$('.registerBox select').eq(i);
		var $val=$input.val();
		if($val==""){
			m++;
			$input.next("span").html("This field is required");
		}else{
			$input.next("span").html("");
		}
	}


	if(m>0){
		$('.registerBox').addClass('haserror');
		mui('#offCanvasContentScroll').scroll().scrollTo(0,0,100);
		return false;
	}else{
		$('.registerBox').removeClass('haserror');
		return true;
	}

}

$('.btnBox a.Register').on('tap',function(){
	var w=$(window).width();
	isNull();
	var arr={
		hs_passwd:$('#pwd').val(),
		hs_cfrm_passwd:$('#againPwd').val(),
		hs_title_id:$('#title').val(),
		hs_octopus_no:$('#cNumber').val(),
		hs_first_name:$('#name').val(),
		hs_family_name:$('#surname').val(),
		hs_date_of_birth:birth_date,
		hs_web_loc_id:"en",
		hs_view_pdt_mode:"IMAGE",
		hs_addr_name:$('#nikeName').val(),
		hs_addr_1:$('#floor').val(),
		hs_addr_2:$('#building').val(),
		hs_addr_3:$('#estate').val(),
		hs_addr_4:$('#street').val(),
		hs_region_id:$('#Region').val(),
		hs_district_id:$('#District').val(),
		hs_main_phone:$('#phone').val(),
		hs_addi_phone:'',
		hs_fax:'12345678',
		hs_main_email:$('#email').val(),
		hs_addi_email:'',
		hs_cus_type:$('#shopType option:selected').val(),
		hs_comp_name:$('#companyName').val(),
		hs_opt_in_promo:"",
		hs_opt_in_improve:improve,
		hs_agree_tnc_version_id:"",
		hvs_qtn_id:"1:1",
		hs_refer_id:"",
		hs_captcha:$("#validate").val(),
		hs_action_id:"save",
		hf_s_id:"WD11",
		hf_srv_id:"Cv_jcus_reg",
		button:"Save",
		hs_inv_loc_id:"@@@"
	};
	getAjaxRequest(arr,function(result){
		console.log(result);
		if(result.bs_result==BS_RESULT.OK){
			$('.registerBox').removeClass('haserror');
			dialog('Registration Completed', 'Thank you.<br/>'+$('#title option:selected').text()+' '+$('#name').val()+' '+$('#surname').val()+', your account has been successfully registered. Happy Shopping!', 3);
			var $dialog = $('#dialog');
			$dialog.show();
			$('.comfirmBtn').html('Start Shopping');
			$('.giveBtn').html('My Account');
			$('.weui_dialog_ft').addClass('setWidth');
			$('.mui-icon-closeempty').hide();
			if(w<375){
				$('.weui_dialog_ft a').css({'padding':'0 8px','margin':'0 5px'});
			}else{
				$('.weui_dialog_ft a').css({'padding':'0 10px','margin':'0 6px'});
			}
			$dialog.find('.comfirmBtn').one('click', function() {
				$dialog.hide();
				$dialog.remove();
				location.href=MPJ.base+"/index.jsp";
			})
			$dialog.find('.giveBtn').off();
			$dialog.find('.giveBtn').one('click', function() {
				$dialog.hide();
				$dialog.remove();
				location.href=MPJ.base+"/pages/account/index.jsp";
			})
		}else{
			$('.registerBox').addClass('haserror');
			$('.error span').html(result.bs_message);
			mui('#offCanvasContentScroll').scroll().scrollTo(0,0,100);
			var date=new Date();
			$('.validate').html('<img src="http://203.189.170.43/mpj2/Captcha.jpg?'+date.getTime()+'" />');
			if(result.bs_err_code=="C0079"){
				$('#validate').addClass('err');
				$('#validate').next("span").html(result.bs_message);
			}else{
				$('#validate').removeClass('err');
				$('#validate').next("span").html('');
			}
			if(result.bs_err_code=="Z0204"){
				$('#againPwd').addClass('err');
				$('#againPwd').next("span").html(result.bs_message);
			}else{
				$('#againPwd').removeClass('err');
				$('#againPwd').next("span").html('');
			}
			if(result.bs_err_code=="Z0055"){
				$('#pwd').addClass('err');
				$('#pwd').next("span").html(result.bs_message);
			}else{
				$('#pwd').removeClass('err');
				$('#pwd').next("span").html('');
			}
			if(result.bs_err_code=="Z0045"){
				$('#phone').addClass('err');
				$('#phone').next("span").html(result.bs_message);
			}else{
				$('#phone').removeClass('err');
				$('#phone').next("span").html('');
			}
			if(result.bs_err_code=="Z0078"){
				$('#companyName').addClass('err');
				$('#companyName').next("span").html(result.bs_message);
			}else{
				$('#companyName').removeClass('err');
				$('#companyName').next("span").html('');
			}
			if(result.bs_err_code=="Z0227" || result.bs_err_code=="Z0143"){
				$('#email').addClass('err');
				$('#email').next("span").html(result.bs_message);
			}else{
				$('#email').removeClass('err');
				$('#email').next("span").html('');
			}
			if(result.bs_err_code=="Z0228"){
				$('#cNumber').addClass('err');
				$('#cNumber').next("span").html(result.bs_message);
			}else{
				$('#cNumber').removeClass('err');
				$('#cNumber').next("span").html('');
			}
			if(result.bs_err_code=="Z0142"){
				$('#phone').addClass('err');
				$('#phone').next("span").html(result.bs_message);
			}else{
				$('#phone').removeClass('err');
				$('#phone').next("span").html('');
			}
		}
	},null,true);
})

$(function () {
	mui(".mui-scroll-wrapper").scroll().refresh();
})



